import time
from selenium import webdriver

# Настройка параметров ChromeOptions
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
options.add_argument(
    "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 "
    "Safari/537.36")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

# hyperskill locators
HYPERSKILL_RESET_PASSWORD_LOCATOR = ("xpath", "//a[contains(text(),'Reset password')]")
HYPERSKILL_EXPLORE_LINK_LOCATOR = ("xpath", "//a[contains(text(),'Explore')]")

# avito locators
AVITO_NO_LOCATION_LOCATOR = ("xpath", "//span[contains(text(),'Оставить так')]")
AVITO_ALL_CATEGORIES = ("xpath", "//span[contains(text(),'Все категории')]")
AVITO_FIND_INPUT_LOCATOR = ("xpath", "//input[@data-marker='search-form/suggest']")
AVITO_FIND_BUTTON_LOCATOR = ("xpath", "//button[@data-marker='search-form/submit-button']")

# ozon locators
OZON_FIND_INPUT = ("xpath", "//input[@name='text']")
OZON_FIND_BUTTON = ("xpath", "//button[@aria-label='Поиск']")

# Открываем вкладочки
for url in ["https://hyperskill.org/login", "https://www.avito.ru/", "https://www.ozon.ru/"]:
    driver.get(url)
    driver.switch_to.new_window('tab')

# Проходим и получаем тайтл
for window in driver.window_handles:
    driver.switch_to.window(window)
    time.sleep(3)
    print(driver.title)

# выполняем какие то активности
for window in driver.window_handles:
    driver.switch_to.window(window)
    url = driver.current_url
    if url == "https://hyperskill.org/login":
        driver.find_element(*HYPERSKILL_RESET_PASSWORD_LOCATOR).click()
        time.sleep(2)
        driver.back()
        driver.find_element(*HYPERSKILL_EXPLORE_LINK_LOCATOR).click()
        time.sleep(1)
        driver.find_element(*HYPERSKILL_EXPLORE_LINK_LOCATOR).click()
    elif url == "https://www.avito.ru/":
        driver.find_element(*AVITO_NO_LOCATION_LOCATOR).click()
        driver.find_element(*AVITO_ALL_CATEGORIES).click()
        time.sleep(1)
        driver.find_element(*AVITO_ALL_CATEGORIES).click()
        driver.find_element(*AVITO_FIND_INPUT_LOCATOR).send_keys("Книга")
        driver.find_element(*AVITO_FIND_BUTTON_LOCATOR).click()
    elif url == "https://www.ozon.ru/":
        driver.find_element(*OZON_FIND_INPUT).send_keys("паровая щетка для кошек")
        driver.find_element(*OZON_FIND_BUTTON).click()
        time.sleep(2)